<?php

class PostController extends BackendController
{
    public $active = "article";

    public function actionArticle()
    {
        $action_typpe = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
        if (isset($_REQUEST['post_id'])&&$action_typpe=="delete") {
            $post_id = $_REQUEST['post_id'];
            $this->connection->createCommand("delete from rk_post where  post_id = :post_id")
                ->bindParam(":post_id", $post_id)
                ->execute();
        }else if($action_typpe == "insert" || $action_typpe == "update" || $action_typpe == "insertImage" || $action_typpe == "updateImage"){
            $post_id = $_POST['post_id'];
            $post_type = $_POST['post_type'];
            $title = $_POST['title'];
            $content = strip_tags($_POST['content']);
            $category_id = $_POST['category_id'];
            $action_typpe = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
            $userinfo = $this->getUser();

            if($action_typpe == 'insert' ){
                $this->connection->createCommand("insert into rk_post (post_type, title, content, user_id,category_id) values (:post_type, :title, :content, :user_id,:category_id)")
                    ->bindParam(":post_type", $post_type)
                    ->bindParam(":title", $title)
                    ->bindParam(":content", $content)
                    ->bindParam(":category_id", $category_id)
                    ->bindParam(":user_id", $userinfo['user_id'])
                    ->execute();
                $post_id = $this->connection->getLastInsertID();
                $this->data['message'] = AppCode::$success;
            }else if($action_typpe=='update'){
                $this->connection->createCommand("update rk_post set title = :title,content=:content,category_id=:category_id,user_id=:user_id where  post_id = :post_id")
                    ->bindParam(":title", $title)
                    ->bindParam(":content", $content)
                    ->bindParam(":category_id", $category_id)
                    ->bindParam(":user_id", $userinfo['user_id'])
                    ->bindParam(":post_id", $post_id)
                    ->execute();
                $this->data['message'] = AppCode::$success;
            }
        }

        if($action_typpe == null || $action_typpe == "insert" || $action_typpe == "update" || $action_typpe =="delete" ){
            $this->data['list'] = $this->connection->createCommand("SELECT * from rk_post where post_type = 'article'")
                ->queryAll();
            $this->render("article", $this->data);
        }
    }

    public function actionArticlewrite()
    {
        $this->active = "articlewrite";

        $post_id = 0;
        $this->data['post'] = null;
        if (isset($_REQUEST['post_id'])) {
            $post_id = $_REQUEST['post_id'];
            $this->data['post'] = $this->connection->createCommand("SELECT * from rk_post where post_type = 'article' and post_id = :post_id")
                ->bindParam(":post_id", $post_id)
                ->queryRow();
        }
        $this->data['post_id'] = $post_id;

        $this->data['categorys'] = $this->connection->createCommand("SELECT * from rk_category where category_type = 'article'")
            ->queryAll();

        $this->render("articlewrite", $this->data);
    }

    public function actionPagewrite()
    {
        $this->active = "pagewrite";

        $post_id = 0;
        $this->data['post'] = null;
        if (isset($_REQUEST['post_id'])) {
            $post_id = $_REQUEST['post_id'];
            $this->data['post'] = $this->connection->createCommand("SELECT * from rk_post where post_type = 'page' and post_id = :post_id")
                ->bindParam(":post_id", $post_id)
                ->queryRow();
        }
        $this->data['post_id'] = $post_id;

        $this->data['categorys'] = $this->connection->createCommand("SELECT * from rk_category where category_type = 'page'")
            ->queryAll();

        $this->render("pagewrite", $this->data);
    }



    public function actionTag()
    {
        $this->active = "tag";

        $this->data['list'] = $this->connection->createCommand("SELECT * from rk_post where post_type = 'tag'")
            ->queryAll();

        $this->render("tag", $this->data);
    }

    public function actionImage()
    {
        $this->active = "image";

        $action_typpe = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
        if ($action_typpe == 'insert' || $action_typpe=="update") {

            $post_id = $_POST['post_id'];
            $post_type = $_POST['post_type'];
            $title = $_POST['title'];
            $content = strip_tags($_POST['content']);
            $category_id = $_POST['category_id'];
            $action_typpe = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
            $userinfo = $this->getUser();

            if($action_typpe == 'insert'){
                $this->connection->createCommand("insert into rk_post (post_type, title, content, user_id,category_id) values (:post_type, :title, :content, :user_id,:category_id)")
                    ->bindParam(":post_type", $post_type)
                    ->bindParam(":title", $title)
                    ->bindParam(":content", $content)
                    ->bindParam(":category_id", $category_id)
                    ->bindParam(":user_id", $userinfo['user_id'])
                    ->execute();
                $post_id = $this->connection->getLastInsertID();
                $this->data['message'] = AppCode::$success;
            }else if( $action_typpe == 'update'){
                $this->connection->createCommand("update rk_post set title = :title,content=:content,category_id=:category_id,user_id=:user_id where  post_id = :post_id")
                    ->bindParam(":title", $title)
                    ->bindParam(":content", $content)
                    ->bindParam(":category_id", $category_id)
                    ->bindParam(":user_id", $userinfo['user_id'])
                    ->bindParam(":post_id", $post_id)
                    ->execute();
                $this->data['message'] = AppCode::$success;
            }

            if($action_typpe == 'insert'|| $action_typpe == 'update'){
                $image = CUploadedFile::getInstanceByName("image");
                if ($image != null) {
                    $extensionName = $image->getExtensionName();
                    if ($extensionName == 'jpg' || $extensionName == 'png' || $extensionName == 'gif') {
                        $size = $image->getSize() / 1024 / 1024;
                        if ($size > 2) {
                            throw new Exception(AppCode::$error_imageIsToBig['message'], AppCode::$error_imageIsToBig['code']);
                        } else {
                            $absolutePath = $_SERVER['DOCUMENT_ROOT'] . "/attachment/image";
                            //插入图片
                            $filePath = $absolutePath;
                            $date = date('Ymd');
                            $filePath = $filePath . '/' . $date;
                            if (!file_exists($filePath)) {
                                mkdir($filePath);
                            }

                            $filename = md5(rand(1, 10000) . rand(1, 10000)) . '.' . $extensionName;
                            $imagePath = 'attachment/image/' . $date . '/' . $filename;
                            $filePath .= '/' . $filename;

                            $image->saveAs($filePath);

                            //删除原来的图片，增加新的图片
                            $postMetas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                                ->bindParam(":post_id", $post_id)->queryAll();
                            $metas = array();
                            if (sizeof($postMetas) > 0) {
                                foreach ($postMetas as $meta) {
                                    $metas[$meta['meta_key']] = $meta['meta_value'];
                                }
                                $lastImage = $_SERVER['DOCUMENT_ROOT'] . '/' . $metas['imagePath'];
                                if (file_exists($lastImage)) {
                                    unlink($lastImage);
                                }
                            }
                            $this->connection->createCommand("delete from rk_postmeta where post_id = :post_id")
                                ->bindParam(":post_id", $post_id)
                                ->execute();

                            $this->connection->createCommand("insert into rk_postmeta (post_id,meta_key,meta_value) values (:post_id,'imagePath',:meta_value)")
                                ->bindParam(":post_id", $post_id)
                                ->bindParam(":meta_value", $imagePath)
                                ->execute();
                        }
                    } else {
                        throw new Exception(AppCode::$error_isNotImage['message'], AppCode::$error_isNotImage['code']);
                    }
                }
            }

        } else if (isset($_REQUEST['post_id'])&&$action_typpe=='delete') {
            $post_id = $_REQUEST['post_id'];
            $this->connection->createCommand("delete from rk_post where  post_id = :post_id")
                ->bindParam(":post_id", $post_id)
                ->execute();
            $postMetas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                ->bindParam(":post_id", $post_id)->queryAll();
            $metas = array();
            if (sizeof($postMetas) > 0) {
                foreach ($postMetas as $meta) {
                    $metas[$meta['meta_key']] = $meta['meta_value'];
                }
                if($metas['imagePath']!=null&&$metas['imagePath']!=''){
                    $lastImage = $_SERVER['DOCUMENT_ROOT'] . '/' . $metas['imagePath'];
                    if (file_exists($lastImage)) {
                        unlink($lastImage);
                    }
                }
            }
        }

        $this->data['list'] = $this->connection->createCommand("SELECT * from rk_post where post_type = 'image'")
            ->queryAll();

        $this->render("image", $this->data);
    }

    public function actionFile()
    {
        $this->active = "file";

        $action_typpe = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
        if ($action_typpe == 'insert' || $action_typpe=="update") {

            $post_id = $_POST['post_id'];
            $post_type = $_POST['post_type'];
            $title = $_POST['title'];
            $content = strip_tags($_POST['content']);
            $category_id = $_POST['category_id'];
            $action_typpe = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
            $userinfo = $this->getUser();

            if($action_typpe == 'insert'){
                $this->connection->createCommand("insert into rk_post (post_type, title, content, user_id,category_id) values (:post_type, :title, :content, :user_id,:category_id)")
                    ->bindParam(":post_type", $post_type)
                    ->bindParam(":title", $title)
                    ->bindParam(":content", $content)
                    ->bindParam(":category_id", $category_id)
                    ->bindParam(":user_id", $userinfo['user_id'])
                    ->execute();
                $post_id = $this->connection->getLastInsertID();
                $this->data['message'] = AppCode::$success;
            }else if( $action_typpe == 'update'){
                $this->connection->createCommand("update rk_post set title = :title,content=:content,category_id=:category_id,user_id=:user_id where  post_id = :post_id")
                    ->bindParam(":title", $title)
                    ->bindParam(":content", $content)
                    ->bindParam(":category_id", $category_id)
                    ->bindParam(":user_id", $userinfo['user_id'])
                    ->bindParam(":post_id", $post_id)
                    ->execute();
                $this->data['message'] = AppCode::$success;
            }

            if($action_typpe == 'insert'|| $action_typpe == 'update'){
                $file = CUploadedFile::getInstanceByName("file");
                if ($file != null) {
                    $extensionName = $file->getExtensionName();
                    $absolutePath = $_SERVER['DOCUMENT_ROOT'] . "/attachment/file";
                    //插入图片
                    $filePath = $absolutePath;
                    $date = date('Ymd');
                    $filePath = $filePath . '/' . $date;
                    if (!file_exists($filePath)) {
                        mkdir($filePath);
                    }

                    $filename = md5(rand(1, 10000) . rand(1, 10000)) . '.' . $extensionName;
                    $imagePath = 'attachment/file/' . $date . '/' . $filename;
                    $filePath .= '/' . $filename;

                    $file->saveAs($filePath);

                    $postMetas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                        ->bindParam(":post_id", $post_id)->queryAll();
                    $metas = array();
                    if (sizeof($postMetas) > 0) {
                        foreach ($postMetas as $meta) {
                            $metas[$meta['meta_key']] = $meta['meta_value'];
                        }
                        $lastImage = $_SERVER['DOCUMENT_ROOT'] . '/' . $metas['filePath'];
                        if (file_exists($lastImage)) {
                            unlink($lastImage);
                        }
                    }
                    $this->connection->createCommand("delete from rk_postmeta where post_id = :post_id")
                        ->bindParam(":post_id", $post_id)
                        ->execute();

                    $this->connection->createCommand("insert into rk_postmeta (post_id,meta_key,meta_value) values (:post_id,'filePath',:meta_value)")
                        ->bindParam(":post_id", $post_id)
                        ->bindParam(":meta_value", $imagePath)
                        ->execute();
                }
            }

        } else if (isset($_REQUEST['post_id'])&&$action_typpe=='delete') {
            $post_id = $_REQUEST['post_id'];
            $this->connection->createCommand("delete from rk_post where  post_id = :post_id")
                ->bindParam(":post_id", $post_id)
                ->execute();
            $postMetas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                ->bindParam(":post_id", $post_id)->queryAll();
            $metas = array();
            if (sizeof($postMetas) > 0) {
                foreach ($postMetas as $meta) {
                    $metas[$meta['meta_key']] = $meta['meta_value'];
                }
                if($metas['filePath']!=null&&$metas['filePath']!=''){
                    $lastImage = $_SERVER['DOCUMENT_ROOT'] . '/' . $metas['filePath'];
                    if (file_exists($lastImage)) {
                        unlink($lastImage);
                    }
                }
            }
        }

        $this->data['list'] = $this->connection->createCommand("SELECT * from rk_post where post_type = 'file'")
            ->queryAll();

        $this->render("file", $this->data);
    }

    public function actionAttachmentimage()
    {
        $this->active = "attachmentimage";

        $post_id = 0;
        $this->data['post'] = null;
        if (isset($_REQUEST['post_id'])) {
            $post_id = $_REQUEST['post_id'];
            $this->data['post'] = $this->connection->createCommand("SELECT * from rk_post where post_type = 'image' and post_id = :post_id")
                ->bindParam(":post_id", $post_id)
                ->queryRow();
            $postMetas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                ->bindParam(":post_id", $post_id)->queryAll();
            $meta = array();
            if (sizeof($postMetas) > 0) {
                foreach ($postMetas as $meta) {
                    $meta[$meta['meta_key']] = $meta['meta_value'];
                }
            }
            $this->data['meta'] = $meta;
        }
        $this->data['post_id'] = $post_id;

        $this->data['categorys'] = $this->connection->createCommand("SELECT * from rk_category where category_type = 'image'")
            ->queryAll();

        $this->render("attachmentimage", $this->data);
    }

    public function actionAttachmentfile()
    {
        $this->active = "attachmentfile";

        $post_id = 0;
        $this->data['post'] = null;
        if (isset($_REQUEST['post_id'])) {
            $post_id = $_REQUEST['post_id'];
            $this->data['post'] = $this->connection->createCommand("SELECT * from rk_post where post_type = 'file' and post_id = :post_id")
                ->bindParam(":post_id", $post_id)
                ->queryRow();
            $postMetas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                ->bindParam(":post_id", $post_id)->queryAll();
            $meta = array();
            if (sizeof($postMetas) > 0) {
                foreach ($postMetas as $meta) {
                    $meta[$meta['meta_key']] = $meta['meta_value'];
                }
            }
            $this->data['meta'] = $meta;
        }
        $this->data['post_id'] = $post_id;

        $this->data['categorys'] = $this->connection->createCommand("SELECT * from rk_category where category_type = 'file'")
            ->queryAll();

        $this->render("attachmentfile", $this->data);
    }

    public function actionPage()
    {
        $this->active = "page";

        $action_type = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;

        if ($action_type == 'edit') {
            $post_id = $_POST['post_id'];
            $post_type = 'page';
            $title = $_POST['title'];
            $content = strip_tags($_POST['content']);
            $userinfo = $this->getUser();
            if ($post_id == 0) {
                //新增
                $this->connection->createCommand("insert into rk_post (post_type, title, content, user_id) values (:post_type, :title, :content, :user_id)")
                    ->bindParam(":post_type", $post_type)
                    ->bindParam(":title", $title)
                    ->bindParam(":content", $content)
                    ->bindParam(":user_id", $userinfo['user_id'])
                    ->execute();
            } else {
                //修改
                $this->connection->createCommand("update rk_post set title = :title,content=:content,user_id=:user_id where  post_id = :post_id")
                    ->bindParam(":title", $title)
                    ->bindParam(":content", $content)
                    ->bindParam(":user_id", $userinfo['user_id'])
                    ->bindParam(":post_id", $post_id)
                    ->execute();
            }
            $this->data['message'] = AppCode::$success;
        } else if ($action_type == 'del') {
            if (isset($_REQUEST['post_id'])) {
                $post_id = $_REQUEST['post_id'];
                $this->connection->createCommand("delete from rk_post where  post_id = :post_id")
                    ->bindParam(":post_id", $post_id)
                    ->execute();
            }
        }

        $this->data['list'] = $this->connection->createCommand("SELECT * from rk_post where post_type = 'page'")
            ->queryAll();

        $this->render("page", $this->data);
    }

    public function actionCategory()
    {
        $this->active = "category";

        $action_type = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
        if ($action_type == 'save') {
            $category = $_REQUEST['category'];
            $this->connection->createCommand("insert into rk_category (category_type,categoryname) values ('article',:categoryname)")
                ->bindParam(":categoryname", $category)
                ->execute();
        } else if ($action_type == 'del') {
            $delete_ategory_id = $_REQUEST['delete_ategory_id'];
            $this->connection->createCommand("delete from rk_category where category_id = :category_id")
                ->bindParam(":category_id", $delete_ategory_id)
                ->execute();
        } else if ($action_type == 'edit') {
            $category_id = $_REQUEST['category_id'];
            $category = $this->connection->createCommand("select * from rk_category where category_id = :category_id")
                ->bindParam(":category_id", $category_id)
                ->queryRow();
            $this->data['editcategory'] = $category;
        } else if ($action_type == 'saveedit') {
            $category_id = $_REQUEST['category_id'];
            $category = $_REQUEST['category'];
            $this->connection->createCommand("update rk_category set categoryname = :category where category_id = :category_id")
                ->bindParam(":category_id", $category_id)
                ->bindParam(":category", $category)
                ->execute();
        }

        $this->data['categoryarticle'] = $this->connection->createCommand("SELECT * FROM rk_category WHERE category_type = 'article'")
            ->queryAll();

        $this->render("category", $this->data);
    }

    public function actionCategoryimages()
    {
        $this->active = "categoryimages";

        $action_type = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
        if ($action_type == 'save') {
            $category = $_REQUEST['category'];
            $this->connection->createCommand("insert into rk_category (category_type,categoryname) values ('image',:categoryname)")
                ->bindParam(":categoryname", $category)
                ->execute();
        } else if ($action_type == 'del') {
            $delete_ategory_id = $_REQUEST['delete_ategory_id'];
            $this->connection->createCommand("delete from rk_category where category_id = :category_id")
                ->bindParam(":category_id", $delete_ategory_id)
                ->execute();
        } else if ($action_type == 'edit') {
            $category_id = $_REQUEST['category_id'];
            $category = $this->connection->createCommand("select * from rk_category where category_id = :category_id")
                ->bindParam(":category_id", $category_id)
                ->queryRow();
            $this->data['editcategory'] = $category;
        } else if ($action_type == 'saveedit') {
            $category_id = $_REQUEST['category_id'];
            $category = $_REQUEST['category'];
            $this->connection->createCommand("update rk_category set categoryname = :category where category_id = :category_id")
                ->bindParam(":category_id", $category_id)
                ->bindParam(":category", $category)
                ->execute();
        }

        $this->data['categoryarticle'] = $this->connection->createCommand("SELECT * FROM rk_category WHERE category_type = 'image'")
            ->queryAll();

        $this->render("categoryimages", $this->data);
    }

    public function actionCategoryfiles()
    {
        $this->active = "categoryfiles";

        $action_type = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
        if ($action_type == 'save') {
            $category = $_REQUEST['category'];
            $this->connection->createCommand("insert into rk_category (category_type,categoryname) values ('file',:categoryname)")
                ->bindParam(":categoryname", $category)
                ->execute();
        } else if ($action_type == 'del') {
            $delete_ategory_id = $_REQUEST['delete_ategory_id'];
            $this->connection->createCommand("delete from rk_category where category_id = :category_id")
                ->bindParam(":category_id", $delete_ategory_id)
                ->execute();
        } else if ($action_type == 'edit') {
            $category_id = $_REQUEST['category_id'];
            $category = $this->connection->createCommand("select * from rk_category where category_id = :category_id")
                ->bindParam(":category_id", $category_id)
                ->queryRow();
            $this->data['editcategory'] = $category;
        } else if ($action_type == 'saveedit') {
            $category_id = $_REQUEST['category_id'];
            $category = $_REQUEST['category'];
            $this->connection->createCommand("update rk_category set categoryname = :category where category_id = :category_id")
                ->bindParam(":category_id", $category_id)
                ->bindParam(":category", $category)
                ->execute();
        }

        $this->data['categoryarticle'] = $this->connection->createCommand("SELECT * FROM rk_category WHERE category_type = 'file'")
            ->queryAll();

        $this->render("categoryfiles", $this->data);
    }


}